package com.amc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.amc.entity.Merchant;
import com.amc.service.IMerchantService;
import com.amc.service.impl.MerchantServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Servlet implementation class MerchantListServlet
 */
public class MerchantListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ObjectMapper mapper = new ObjectMapper();
	private IMerchantService service = new MerchantServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MerchantListServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		getMerchantList(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		getMerchantList(request, response);
	}
	private void getMerchantList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Map<String, Object> resultMap = new HashMap<>();
		PrintWriter writer = response.getWriter();
		String isBlack = request.getParameter("isBlack");
		String merchantName = request.getParameter("merchantName");
		List<Merchant> merchantList = service.query(isBlack, merchantName);
		if(merchantList.size() > 0){
			resultMap.put("status", "success");
			resultMap.put("msg", "");
			resultMap.put("data", merchantList);
		}else{
			resultMap.put("status", "fail");
			resultMap.put("msg", "暂无数据！");
		}
		
		String json = mapper.writeValueAsString(resultMap);
		writer.write(json);
		writer.close();
	}

}
